package com.bluetoothsms.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MessageSmsList extends BluetoothMessage {

	public MessageSmsList(int threadId, Sms[] s) {
		super(BtMessageType.SmsList, null);

		mData = new String[1 + s.length * Sms.FIELD_COUNT];

		int i = 0;
		mData[i++] = String.valueOf(threadId);

		for (Sms sms : s) {
			mData[i++] = sms.getText();
			mData[i++] = String.valueOf(sms.getDate().getTime());
			mData[i++] = String.valueOf(sms.getType().ordinal());
			mData[i++] = String.valueOf(sms.getThreadId());
			mData[i++] = sms.getNumber();
			mData[i++] = String.valueOf(sms.getStatus().ordinal());
		}
	}

	public MessageSmsList(byte[] data, int length) {
		super(BtMessageType.SmsList, data, length);
	}

	public int getThreadId() {
		return Integer.valueOf(mData[0]);
	}

	public Collection<Sms> getSmsList() {
		List<Sms> list = null;

		if ((mData.length - 1) % Sms.FIELD_COUNT == 0) {
			list = new ArrayList<Sms>((mData.length - 1) / Sms.FIELD_COUNT);

			for (int i = 1; i < mData.length; i += Sms.FIELD_COUNT) {
				list.add(new Sms(mData, i));
			}
		}

		return list;
	}
}
